#include<iostream>
#include<algorithm>
#include<list>
#include<sstream>
#include<vector>
#include<iostream>
#include<fstream>
#include "employee.h"
#include "department.h"
using namespace std;

bool login(){
    string account;
    string password;
    cout <<"请输入账号："<<endl;
    cin >> account;
    cout <<"请输入密码："<<endl;
    cin >> password;

    if(account == "admin" && password == "123"){
        return true;
    }
    return false;
}

//添加员工信息
Employee add_employer(){
    string name;
    cout<<"请输入姓名:"<<endl;
    cin >> name;
    string employee_id;
    cout<<"请输入员工号:"<<endl;
    cin >> employee_id;
    string department;
    cout<<"请输入员工部门:"<<endl;
    cin >> department;
    double basic;//
    cout<<"请输入员工基本工资:"<<endl;
    cin >> basic;
    double reward;//
    cout<<"请输入员工奖励金额:"<<endl;
    cin >> reward;
    string subsidy_item;//
    cout<<"请输入补贴项目:"<<endl;
    cin >> subsidy_item;
    double subsidy;//
    cout<<"请输入补贴金额:"<<endl;
    cin >> subsidy;
    string deduction_item;//
    cout<<"请输入扣款项目:"<<endl;
    cin >> deduction_item;
    double deduction;
    cout<<"请输入扣款金额:"<<endl;
    cin>>deduction;
    Employee employer = Employee( name, employee_id, department, basic, reward, subsidy_item, subsidy, deduction_item, deduction);
    return employer;
}

//删除员工信息
bool delete_employer(vector<Employee>&staff,string e_id){

    for(auto i =0 ; i < staff.size(); ++i){
        if(staff.at(i).get_employeeId() == e_id){
            staff.erase(staff.begin()+i+1);
            return true;
        }
    }
    return false ;

}

//显示所有员工信息
void display_all_employee(vector<Employee>&staff){
    for(auto i =0 ; i < staff.size(); ++i){
        staff.at(i).display();
    }
}

//自定义比较函数
bool compare_by_id( Employee& left, Employee& right){
    return left.get_employeeId() < right.get_employeeId();
}


bool compare_by_name( Employee& left, Employee& right){
    return left.get_name() < right.get_name();
}

//按员工号排序
void order_by_employee_id(vector<Employee>&staff){
    std::sort(staff.begin(),staff.end(),compare_by_id);
    display_all_employee(staff);
}

//按姓名排序
void order_by_employee_name(vector<Employee>&staff){
    std::sort(staff.begin(),staff.end(),compare_by_name);
    display_all_employee(staff);
}

//按员人名查询：
void search_by_name(vector<Employee>&staff){
    string name ;
    cout<<"请输入要查询员工姓名："<<endl;
    cin>>name;
    for(auto i = 0 ; i < staff.size(); ++i){
        if(staff.at(i).get_name()==name){
            staff.at(i).display();
        }
    }
}

//显示固定范围工资信息（包括人数）
void search_by_salary(vector<Employee>&staff){
    double start;
    double end;
    cout<<"请输入起始工资"<<endl;
    cin>>start;
    cout<<"请输入结束工资"<<endl;
    cin>>end;
    int count = 0 ; //记录人数
    for(auto i = 0 ; i < staff.size(); ++i){
        double salary = staff.at(i).sum_salary();
        if(salary >=start && salary <= end){
            count ++;
            staff.at(i).display();
        }
    }
    cout<<"一共有"<<count<<"人数"<<endl;
}

//显示各部门工资总数
void display_department_salary(vector<Employee>&staff){
    vector<Department> departments ;
    for(auto i = 0 ; i < staff.size(); ++i){
        bool flag = true;//判断是否已经保存部门
        for(auto j = 0 ; j < departments.size(); ++j){
            if(departments.at(j).get_name()==staff.at(i).get_department()){
                departments.at(j).set_salary(staff.at(i).sum_salary());
                flag = false;
                break;
            }
        }
        if(flag){
            Department d = Department(staff.at(i).get_department(),staff.at(i).sum_salary());
            departments.push_back(d);
        }
    }

    for(auto i = 0 ; i < departments.size(); ++i){
        departments.at(i).display();
    }

}

//修改员工信息
void modify(vector<Employee>&staff){
    cout<<"请输入需要需改的员工工号"<<endl;
    string e_id ;
    cin >> e_id;
    if(delete_employer(staff,e_id)){
        Employee e = add_employer();
        staff.push_back(e);
        cout<<"修改成功"<<endl;
    }else{
        
        cout<<"查找不到该用户信息"<<endl;
    }

}
int menu(){
    int choice;
    cout <<"++++++++++++++++++++++++++++++"<<endl;
    cout <<"1.添加员工信息："<<endl;
    cout <<"2.删除员工信息："<<endl;
    cout <<"3.显示所有员工信息："<<endl;
    cout <<"4.按员工号排序："<<endl;
    cout <<"5.按员工姓名排序："<<endl;
    cout <<"6.按员人名查询："<<endl;
    cout <<"7.显示固定范围工资信息（包括人数）："<<endl;
    cout <<"8.显示各部门工资总数："<<endl;
    cout <<"9.修改员工信息："<<endl;
    cout <<"0.退出："<<endl;
    cout <<"++++++++++++++++++++++++++++++"<<endl;
    cin>>choice;
    return choice;
}
int main(){
    if(login()){
        vector<Employee> staff;
        Employee e ;
        string e_id;
        while (int choice = menu())
        {
            switch (choice)
            {
            case 1:
                e = add_employer();
                staff.push_back(e);
                break;
            case 2:
                cout<<"请输入要删除的用户ID"<<endl;
                cin>>e_id;
                if(delete_employer(staff,e_id)){
                    cout<<"删除成功"<<endl;
                }else{
                    cout<<"删除失败，用户不存在"<<endl;
                }
                break;
            case 3:
                display_all_employee(staff);
                break;
            case 4:
                order_by_employee_id(staff);
                break;
            case 5:
                order_by_employee_name(staff);
                break;
            case 6:
                search_by_name(staff);
                break;
            case 7:
                search_by_salary(staff);
                break;
            case 8:
                display_department_salary(staff);
                break;
            case 9:
                modify(staff);
                break;
            default:
                break;
            }
        }
        
    }else{
        cout <<"登录失败，已退出！！！"<<endl;
    }
}